package nix.jdbc;

import java.sql.SQLException;
import java.util.List;

import nix.demo2.domain.User;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;

public class Test5 {
	private AccountDao accountDao = new AccountDao();
	private UserDao userDao = new UserDao();

	@Test
	public void fun1() throws Exception {
		try {
			JdbcUtils.beginTranscation();
			accountDao.update("ww", -100);
			accountDao.update("zs", 100);
			JdbcUtils.commitTranscation();
		} catch (Exception e) {
			JdbcUtils.rollbackTranscation();
			throw e;
		}
	}
	
	@Test
	public void fun2() throws SQLException {
		List<User> list = userDao.queryAll();
		System.out.println(list);
	}
}

class AccountDao {
	public void update(String name, int amount) throws SQLException {
		QueryRunner qr = new TxQueryRunner();
		qr.update("update account set balance = balance + ? where name = ?",
				amount, name);
	}	
}

class UserDao {
	public List<User> queryAll() throws SQLException {
		QueryRunner qr = new TxQueryRunner();
		return qr.query("select * from t_user", new BeanListHandler<User>(User.class));
	}
}